import logging

import config


class RegLoginApi:

    def __init__(self, session):
        self.session = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        url = config.BASE_HOST + "/common/public/verifycode1/{}".format(r)
        res = self.session.get(url=url)
        logging.info("获取注册图片验证码的响应状态码：{}".format(res.status_code))
        return res

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone_v, imgVerifyCode_v, type_v="reg"):
        url = config.BASE_HOST + "/member/public/sendSms"
        # 定义一个字典变量，用来接收请求体数据
        form_dict = {"phone": phone_v, "imgVerifyCode": imgVerifyCode_v, "type": type_v}
        res = self.session.post(url=url, data=form_dict)
        logging.info("获取注册短信验证码的响应体数据：{}".format(res.json()))
        return res

    # 注册接口
    def user_register(self, phone_v, password_v, verifycode_v, phone_code_v, dy_server_v="on", invite_phone=None):
        url = config.BASE_HOST + "/member/public/reg"
        # 定义一个字典变量，用来接收请求体数据
        form_dict = {"phone": phone_v, "password": password_v, "verifycode": verifycode_v, "phone_code": phone_code_v,
                     "dy_server": dy_server_v, "invite_phone": invite_phone}
        res = self.session.post(url=url, data=form_dict)
        logging.info("注册的响应体数据：{}".format(res.json()))
        return res
    # 登录接口
    def user_login(self, keywords_v, password_v):
        url = config.BASE_HOST + "/member/public/login"
        # 定义一个字典变量，用来接收请求体数据
        form_dict = {"keywords": keywords_v, "password": password_v}
        res = self.session.post(url=url, data=form_dict)
        logging.info("登录的响应体数据：{}".format(res.json()))
        return res